package com.heaven.controller;

import com.heaven.anno.Log;
import com.heaven.pojo.PageBean;
import com.heaven.pojo.Result;
import com.heaven.pojo.User;
import com.heaven.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@RestController
@CrossOrigin(origins="http://localhost:7000")
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    //查询所有数据并分页展示
    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name){
        log.info("分页查询, 参数: {},{},{}",page,pageSize,name);
        //调用service分页查询
        PageBean pageBean = userService.page(page,pageSize,name);
        return Result.success(pageBean);
    }



    //根据id删除用户
    @Log
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("删除操作， id:{}",id )  ;
        userService.delete(id);
        return Result.success();
    }

    //新增成员--新建
    @Log
    @PostMapping
    public Result add_re(@RequestBody User user){
        log.info("新建成员:{}",user);
        if(user.getPassword() == null){
            user.setPassword("123456");
        }
        userService.add__re(user);
        return Result.success();
    }

    //编辑更新成员信息
    @Log
    @PutMapping
    public Result update(@RequestBody User user){
        log.info("更新员工数据:{}",user);
        userService.update(user);
        return Result.success();
    }
}
